#!/user/bin/env node

const log = console.log,
      http = require('http'),
      url = require('url');

let addr = process.argv[2] || 'http://www.sian.com/';

function opt(addr){
  let options = url.parse(addr);
  options.headers = {'User-Agent':'05-readirection.js'};

  return options;
}

function get(options){
  http.get(options,(res)=>{
    log(`HTTP/${res.httpVersion} ${res.statusMessage} ${res.statusCode}`);
    log(res.headers);
    log('');

    if(res.statusCode < 400 && res.statusCode >= 300){
      get(opt(res.headers.location));
    }else{
      res.pipe(process.staout);
    }
  });
}

get(opt(addr));
